package com.itheima.service.impl;

/*
 *@Create Date:  2019/11/20    20:17
 *
 *@Description:
 */


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuDao;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//查询条件

        PageHelper.startPage(currentPage, pageSize);

        Page<Menu> page = menuDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Menu> rows = page.getResult();
        return new PageResult(total, rows);
    }


    //新增才菜单
    @Override
    public void add(Menu menu, Integer[] menuIds) {
        menuDao.add(menu);
        Integer menuId = menu.getId();
        if (menuIds != null && menuIds.length > 0) {
            Map<String, Integer> map = new HashMap<>();
            for (Integer id : menuIds) {
                map.put("menuId", menuId);
                map.put("id", id);
                menuDao.addIds(map);
            }
        }
    }

    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    @Override
    public void edit(Menu menu, Integer[] menuIds) {
        menuDao.edit(menu);
        Integer menuId = menu.getId();
        if (menuIds != null && menuIds.length > 0) {
            for (Integer id : menuIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("menuId", id);
                menuDao.addIds(map);
            }
        }
    }

    @Override
    public Menu findById(Integer id) {
       return menuDao.findById(id);
    }

    @Override
    public void delete(Integer id) {
        menuDao.delete(id);
    }

    @Override
    public List<Integer> findAssociation(Integer id) {
        return menuDao.findAssociation(id);
    }
    public int findUserId(String username) {
        return menuDao.findUserId(username);
    }

    @Override
    public List<Integer> findRoleId(int userId) {
        return menuDao.findRoleId(userId);
    }

    @Override
    public Set<Integer> findMenuId(int roleId) {
        return menuDao.findMenuId(roleId);
    }

    @Override
    public List<Menu> findByParentMenuId(Integer parentMenuId,Set<Integer> menuIds) {
        List<Menu> list= menuDao.findByParentMenuId(parentMenuId);
        List<Menu> list1 = new ArrayList<>();
        for (Menu menu : list) {
            for (Integer menuId : menuIds) {
                if (menu.getId() == menuId) {
                    list1.add(menu);
                }
            }
        }
        return list1;
    }


}
